package gob.osinerg.emergencia.tag;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ControlTagUtil implements Tag{

	private Log log = LogFactory.getLog(ControlTagUtil.class);
	
	private String DEPARTAMENTO="DEPARTAMENTO_OPCION";
	private String PROVINCIA="PROVINCIA_OPCION";
	private String DISTRITO="DISTRITO_OPCION";
	
	private PageContext pageContext;

	private Object opcion;
	
	private Object formBean;

	private Tag parent;
	
	private String REQUERIDO_SI = "SI";
	
	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}

	public int doStartTag() throws JspException {
		JspWriter out=pageContext.getOut();
		
		String className = StringUtils.EMPTY;
		try {
			if (opcion != null){
				opcion = (Map)opcion;
			}
			className = "gob.osinerg.emergencia.tag" + "." + BeanUtils.getProperty(opcion, "TAG");

		} catch (Exception e) {
			log.error(e);
		}
		System.out.println("Nombre de la Clase :"+className);
		if(StringUtils.isNotEmpty(className)){
			try {
				List listaRequeridos = (List)pageContext.getAttribute("listaRequeridos");
				if(listaRequeridos == null){
					listaRequeridos = new ArrayList();
				}
				
				Object obj =  Class.forName(className).newInstance();
				BeanUtils.copyProperty(obj, "maxLength", BeanUtils.getProperty(opcion, "MAXLENGTH"));
				BeanUtils.copyProperty(obj, "name", BeanUtils.getProperty(opcion, "OPCIONNAME"));
				BeanUtils.copyProperty(obj, "rows", BeanUtils.getProperty(opcion, "HEIGTH"));
				BeanUtils.copyProperty(obj, "cols", BeanUtils.getProperty(opcion, "WIDTH"));
				BeanUtils.copyProperty(obj, "size", BeanUtils.getProperty(opcion, "WIDTH"));
				BeanUtils.copyProperty(obj, "label", BeanUtils.getProperty(opcion, "DSCRPCION"));
				BeanUtils.copyProperty(obj, "required", BeanUtils.getProperty(opcion, "REQUERIDO").equals(REQUERIDO_SI)?REQUERIDO_SI:null);
				
					Map mapa = new HashMap();
					
					if (obj instanceof RadioControl || obj instanceof CheckBoxControl){
						mapa.put("ID", BeanUtils.getProperty(opcion, "OPCIONNAME"));
						mapa.put("NAME", BeanUtils.getProperty(opcion, "CATEGORIA"));
						BeanUtils.copyProperty(obj, "value", BeanUtils.getProperty(opcion, "OPCION_ID")+"_"+BeanUtils.getProperty(opcion, "DSCRPCION"));
						//if(PropertyUtils.isWriteable(formBean, "mapa.request."+mapa.get("ID"))){
							//if ((BeanUtils.getProperty(opcion, "OPCION_ID")+"_"+BeanUtils.getProperty(opcion, "DSCRPCION")).equals(BeanUtils.getProperty(formBean, "mapa.request."+mapa.get("ID")))){
								//PropertyUtils.setProperty(obj, "checked",Boolean.TRUE);
						//	}						
						//}
					}else if(obj instanceof ComboBoxControl){
						mapa.put("ID", BeanUtils.getProperty(opcion, "OPCIONNAME")+"_"+BeanUtils.getProperty(opcion, "OPCION_ID"));
						mapa.put("NAME", BeanUtils.getProperty(opcion, "DSCRPCION"));	
						PropertyUtils.setProperty(obj, "id", BeanUtils.getProperty(opcion, "SECC_ID")+"_"+BeanUtils.getProperty(opcion, "OPCION_ID"));
						/*if (BeanUtils.getProperty(opcion, "TIPO").equals(DEPARTAMENTO)){
								BeanUtils.copyProperty(obj, "name", "map.departamento");
								PropertyUtils.setProperty(obj, "listaOpciones", PropertyUtils.getProperty(formBean, "mapList.listaDepartamentos"));
								PropertyUtils.setProperty(obj, "onchange", "doMethod('buscarProvincias')");
								PropertyUtils.setProperty(obj, "selectedValue", PropertyUtils.getProperty(formBean, "mapa.departamento"));
						}else if (BeanUtils.getProperty(opcion, "TIPO").equals(PROVINCIA)){
								BeanUtils.copyProperty(obj, "name", "map.provincia");
								PropertyUtils.setProperty(obj, "listaOpciones", PropertyUtils.getProperty(formBean, "mapList.listaProvincia"));
								PropertyUtils.setProperty(obj, "onchange", "doMethod('buscarDistritos')");
								PropertyUtils.setProperty(obj, "selectedValue", String.valueOf(PropertyUtils.getProperty(formBean, "mapa.provincia")));
							
					    }else if (String.valueOf(BeanUtils.getProperty(opcion, "TIPO")).equals(DISTRITO)){
								BeanUtils.copyProperty(obj, "name", "map.distrito");
								PropertyUtils.setProperty(obj, "listaOpciones", PropertyUtils.getProperty(formBean, "mapList.listaDistrito"));
								PropertyUtils.setProperty(obj, "selectedValue", String.valueOf(PropertyUtils.getProperty(formBean, "mapa.distrito")));

						}*/
						
						
					}else{
						BeanUtils.copyProperty(obj, "name", String.valueOf(BeanUtils.getProperty(opcion, "OPCIONNAME"))+"_"+String.valueOf(BeanUtils.getProperty(opcion, "OPCION_ID")));
						mapa.put("ID", String.valueOf(BeanUtils.getProperty(opcion, "OPCIONNAME"))+"_"+String.valueOf(BeanUtils.getProperty(opcion, "OPCION_ID")));
						mapa.put("NAME", BeanUtils.getProperty(opcion, "DSCRPCION"));
					}
				if (BeanUtils.getProperty(opcion, "REQUERIDO").equals(REQUERIDO_SI)){
					if(!(obj instanceof LabelControl)){
						listaRequeridos.add(mapa);
						pageContext.setAttribute("listaRequeridos", listaRequeridos);
					}
				}
			
				out.println(obj.toString());
			} catch (Exception e) {
				log.error(e);
			}
		}
		return EVAL_BODY_INCLUDE;
	}

	public Tag getParent() {
		return parent;
	}

	public void release() {
		this.pageContext = null;
		this.opcion = null;
		this.log = null;
		this.formBean = null;
	}

	public void setPageContext(PageContext pageContext) {
		this.pageContext = pageContext;
		
	}

	public void setParent(Tag parent) {
		this.parent = parent;
	}

	public Object getOpcion() {
		return opcion;
	}

	public void setOpcion(Object opcion) {
		this.opcion = opcion;
	}

	public Object getFormBean() {
		return formBean;
	}

	public void setFormBean(Object formBean) {
		this.formBean = formBean;
	}
	
	

}